<?php

namespace App\Http\Controllers\Admin\RedEnvelope;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Response\Response;

class RedEnvelopeController extends BaseController {

    /**
     * @api {post} /red-envelope/manager 全局配置
     * @apiVersion 0.1.0
     * @apiName 全服红包-全局配置
     * @apiGroup 全服红包
     *
     * @apiParam {number} rule_type                          规则类型，1：最低接龙  2：最高接龙
     * @apiParam {number} base_money                         保证金
     * @apiParam {number} reward_count                       红包奖品数量
     * @apiParam {number} gift_id                            红包奖品id
     * @apiParam {number} limit_user_level                   限制用户等级
     * @apiParam {number} user_count_limit                   红包游戏房间能够允许的最大人数
     * @apiParam {number} disable                            是否被禁用 0：未禁用 1：禁用
     * @apiParam {number} daily_start_time                   每日活动开始时间(秒，从0到86400)
     * @apiParam {number} daily_end_time                     每日活动结束时间(秒，从0到86400)
     * @apiParam {number} second_daily_start_time            第二轮每日活动开始时间(秒，从0到86400)
     * @apiParam {number} second_daily_end_time              第二轮每日活动结束时间(秒，从0到86400)
     * @apiParam {string} start_time                         红包活动生命周期开始
     * @apiParam {string} end_time                           红包活动生命周期结束
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function edit(Request $request)
    {
        //参数校验
        try {
            $this->validate($request, [
                'rule_type' => 'required|numeric|integer|in:1,2',
                'base_money' => 'required|numeric|integer|min:1',
                'reward_count' => 'required|numeric|integer|min:1',
                'gift_id' => 'required|numeric|exists:mysql.ln_gift,id',
                'limit_user_level' => 'required|numeric|integer|min:0',
                'user_count_limit' => 'required|numeric|integer|min:1',
                'disable' => 'required|numeric|integer|in:0,1',
                'daily_start_time' => 'required|numeric|integer|between:0,86400',
                'daily_end_time' => 'required|numeric|integer|between:0,86400',
                'second_daily_start_time' => 'required|numeric|integer|between:0,86400',
                'second_daily_end_time' => 'required|numeric|integer|between:0,86400',
                'start_time' => 'required|string|date_format:Y-m-d H:i:s',
                'end_time' => 'required|string|date_format:Y-m-d H:i:s',

            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $params = $request->input();

        if (strtotime($params['start_time']) >= strtotime($params['end_time'])) {
            throw new BasicException(10001, "结束时间不能小于或者等于开始时间！");
        }

        if ($params['daily_end_time'] <= $params['daily_start_time']) {
            throw new BasicException(10001, "每日结束时间不能大于或者等于每日开始时间！");
        }

        if ($params['second_daily_end_time'] <= $params['second_daily_start_time']) {
            throw new BasicException(10001, "第二轮每日结束时间不能大于或者等于每日开始时间！");
        }

        $res = app('repo_red_envelope');
        $originGlobalConfig = $res->globalDetail();
        if (!empty($originGlobalConfig)) {
            if ($originGlobalConfig['disable'] == 0
                && ($params['start_time'] != $originGlobalConfig['start_time']
                    || $params['end_time'] != $originGlobalConfig['end_time']
                    || $params['daily_start_time'] != $originGlobalConfig['daily_start_time']
                    || $params['daily_end_time'] != $originGlobalConfig['daily_end_time']
                    || $params['second_daily_start_time'] != $originGlobalConfig['second_daily_start_time']
                    || $params['second_daily_end_time'] != $originGlobalConfig['second_daily_end_time']
                    || $params['user_count_limit'] != $originGlobalConfig['user_count_limit']
                    || $params['reward_count'] != $originGlobalConfig['reward_count']
                    || $params['base_money'] != $originGlobalConfig['base_money']
                    || $params['gift_id'] != $originGlobalConfig['gift_id']
                    || $params['rule_type'] != $originGlobalConfig['rule_type']
                    || $params['limit_user_level'] != $originGlobalConfig['limit_user_level']
                )
            ) {
                throw new BasicException(10001, "只能在关闭游戏时修改其他属性！");
            }
            $params['id'] = $originGlobalConfig->id;
        }

        $rows = $res->updateGlobalConfig($params);
        if (!$rows) {
            throw new BasicException(10001, "修改失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {get} /red-envelope/manager/detail 全局配置详情
     * @apiVersion 0.1.0
     * @apiName 全服红包-全局配置详情
     * @apiGroup 全服红包
     *
     *
     * @apiSuccessExample 成功响应
     *
     *
    {
        "code": 200,
        "data": {
            "config": {
                    "rule_type(规则类型，1：最低接龙  2：最高接龙)": 1,
                    "base_money(保证金)": "999.00",
                    "reward_count(红包奖品数量)": 999,
                    "gift_id(红包奖品id)": 310,
                    "limit_user_level(限制用户等级)": 0,
                    "user_count_limit(红包游戏房间能够允许的最大人数)": 2,
                    "disable(是否被禁用 0：未禁用 1：禁用)": 0,
                    "daily_start_time(每日活动开始时间(秒，从0到86400))": 1,
                    "daily_end_time(每日活动结束时间(秒，从0到86400))": 1000,
                    "second_daily_start_time(第二轮每日活动开始时间(秒，从0到86400))": 1001,
                    "second_daily_end_time(第二轮每日活动结束时间(秒，从0到86400))": 86399,
                    "start_time(红包活动生命周期开始)": "2020-09-01 01:00:00",
                    "end_time(红包活动生命周期结束)": "2021-10-01 00:00:00",
                    }
            },
            "gift_list": [
                {
                    "id": 78,
                    "name": "摘星揽月",
                    "price": 334400,
                    "picture": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/user-dir/MtSk6yyRG7.png"
                }
            ]
        }
    }
     *
     *
     */
    public function detail(Request $request) {

        $config = app('repo_red_envelope')->globalDetail();

        $giftList = app("repo_gift")->gameGiftList(14);

        $result = [
            "config" => $config,
            "gift_list" => $giftList,
        ];

        return Response::sendData($result);
    }

}
